using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using System;
using System.Collections.Generic;
namespace MicroMsg.Scene
{
	public class NetSceneVerifyUser : NetSceneBaseEx<VerifyUserRequest, VerifyUserResponse, VerifyUserRequest.Builder>
	{
		private const string TAG = "NetSceneVerifyUser";
		public void doSceneAddContact(string userName, AddContactScene lastScene)
		{
			Log.smethod_4("NetSceneVerifyUser", "cmd to add contact : " + userName);
			this.doSceneEx(userName, lastScene, VerifyUserOpCode.MM_VERIFYUSER_ADDCONTACT, "");
		}
		public void doSceneSendRequestForAddContact(string userName, string content, AddContactScene lastScene)
		{
			Log.smethod_4("NetSceneVerifyUser", "cmd to verify contact , user= " + userName + ", content = " + content);
			this.doSceneEx(userName, lastScene, VerifyUserOpCode.MM_VERIFYUSER_SENDREQUEST, content);
		}
		public void doSceneAcceptForAddContact(string userName, AddContactScene lastScene)
		{
			Log.smethod_4("NetSceneVerifyUser", "cmd to accept for add contact , user= " + userName);
			this.doSceneEx(userName, lastScene, VerifyUserOpCode.MM_VERIFYUSER_VERIFYOK, "");
		}
		public void doSceneRejectForAddContact(string userName, AddContactScene lastScene)
		{
			Log.smethod_4("NetSceneVerifyUser", "cmd to reject for add contact , user= " + userName);
			this.doSceneEx(userName, lastScene, VerifyUserOpCode.MM_VERIFYUSER_VERIFYREJECT, "");
		}
		public void doSceneAddMultiContacts(List<string> accountList, AddContactScene lastScene, string content)
		{
			this.doSceneMultiEx(accountList, lastScene, VerifyUserOpCode.MM_VERIFYUSER_ADDCONTACT, content);
		}
		public void doSceneSendMultiRequests(List<string> accountList, AddContactScene lastScene, string content)
		{
			this.doSceneMultiEx(accountList, lastScene, VerifyUserOpCode.MM_VERIFYUSER_SENDREQUEST, content);
		}
		private void doSceneMultiEx(List<string> accountList, AddContactScene lastScene, VerifyUserOpCode opCode, string content)
		{
			Log.smethod_4("NetSceneVerifyUser", "cmd to verify or add multi contacts , content = " + content);
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			using (List<string>.Enumerator enumerator = accountList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					Log.smethod_4("NetSceneVerifyUser", "verify user  = " + current);
					this.mBuilder.AddVerifyUserList(VerifyUser.CreateBuilder().SetValue(current).Build());
					this.mBuilder.AddSceneList((uint)lastScene);
				}
			}
			this.mBuilder.VerifyUserListSize = (uint)this.mBuilder.VerifyUserListList.get_Count();
			this.mBuilder.SceneListNum = (uint)this.mBuilder.SceneListList.get_Count();
			this.mBuilder.Opcode = (uint)opCode;
			this.mBuilder.VerifyContent = content;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdID = 44;
			base.endBuilder();
		}
		private void doSceneEx(string account, AddContactScene lastScene, VerifyUserOpCode opCode, string content)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.AddVerifyUserList(VerifyUser.CreateBuilder().SetValue(account).Build());
			this.mBuilder.VerifyUserListSize = (uint)this.mBuilder.VerifyUserListCount;
			this.mBuilder.AddSceneList((uint)lastScene);
			this.mBuilder.SceneListNum = (uint)this.mBuilder.SceneListCount;
			this.mBuilder.Opcode = (uint)opCode;
			this.mBuilder.VerifyContent = content;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdID = 44;
			base.endBuilder();
		}
		protected override void onFailed(VerifyUserRequest request, VerifyUserResponse response)
		{
			this.proceessResult(request, -800000);
		}
		protected override void onSuccess(VerifyUserRequest request, VerifyUserResponse response)
		{
			int arg_0B_0 = response.BaseResponse.Ret;
			this.proceessResult(request, response.BaseResponse.Ret);
		}
		private void proceessResult(VerifyUserRequest request, int result)
		{
			VerifyUserOpCode opcode = (VerifyUserOpCode)request.Opcode;
			AddContactScene lastScene = (AddContactScene)request.SceneListList.get_Item(0);
			string value = request.VerifyUserListList.get_Item(0).Value;
			Log.smethod_4("NetSceneVerifyUser", "verify user return , ret = " + (RetConst)result);
			using (IEnumerator<VerifyUser> enumerator = request.VerifyUserListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					VerifyUser current = enumerator.get_Current();
					Log.smethod_4("NetSceneVerifyUser", "user  = " + current.Value);
				}
			}
			AddContactEventArgs addContactEventArgs = new AddContactEventArgs();
			addContactEventArgs.lastScene = lastScene;
			addContactEventArgs.toUserName = value;
			addContactEventArgs.result = (RetConst)result;
			if (opcode == VerifyUserOpCode.MM_VERIFYUSER_ADDCONTACT)
			{
				if (result == 0)
				{
					Log.smethod_5("NetSceneVerifyUser", "add contact success. username = " + value);
					EventCenter.postEvent(EventConst.ON_NETSCENE_ADDCONTACT_SUCCESS, addContactEventArgs, null);
					return;
				}
				if (result == -44)
				{
					Log.smethod_5("NetSceneVerifyUser", "add contact add contact need verify, username = " + value);
					EventCenter.postEvent(EventConst.ON_NETSCENE_ADDCONTACT_NEEDVIRIFY, addContactEventArgs, null);
					return;
				}
				Log.smethod_5("NetSceneVerifyUser", "add contact failed,   ret = " + (RetConst)result);
				EventCenter.postEvent(EventConst.ON_NETSCENE_ADDCONTACT_ERR, addContactEventArgs, null);
				return;
			}
			else
			{
				if (opcode != VerifyUserOpCode.MM_VERIFYUSER_SENDREQUEST)
				{
					if (opcode == VerifyUserOpCode.MM_VERIFYUSER_VERIFYOK)
					{
						if (result == 0)
						{
							Log.smethod_5("NetSceneVerifyUser", "verifyok success. username = " + value);
							EventCenter.postEvent(EventConst.ON_NETSCENE_ACCEPTVERIFYUSER_SUCCESS, addContactEventArgs, null);
							return;
						}
						Log.smethod_5("NetSceneVerifyUser", "verifyok failed,   ret = " + (RetConst)result);
						EventCenter.postEvent(EventConst.ON_NETSCENE_ACCEPTVERIFYUSER_ERR, addContactEventArgs, null);
					}
					return;
				}
				if (result == 0)
				{
					Log.smethod_5("NetSceneVerifyUser", "send request success. username = " + value);
					EventCenter.postEvent(EventConst.ON_NETSCENE_VERIFYUSER_SUCCESS, addContactEventArgs, null);
					return;
				}
				Log.smethod_5("NetSceneVerifyUser", "send request failed,   ret = " + (RetConst)result);
				EventCenter.postEvent(EventConst.ON_NETSCENE_VERIFYUSER_ERR, addContactEventArgs, null);
				return;
			}
		}
		private static void onHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_ADDCONTACT_SUCCESS)
			{
				Log.smethod_5("NetSceneVerifyUser", "(2/2)on success");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_ADDCONTACT_NEEDVIRIFY)
			{
				Log.smethod_5("NetSceneVerifyUser", "(1/2)on success");
				ServiceCenter.sceneVerifyUser.doSceneSendRequestForAddContact("halen5064", "test", AddContactScene.MM_ADDSCENE_SEARCH_WEIXIN);
			}
		}
	}
}
